﻿using M_Process.API.Appliction.Command.ProductionProcessCommand;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ProductionProcessCommandHandler
{
    /// <summary>
    /// 删除工序命令处理器
    /// </summary>
    public class DeletedProductionProcessCommandHandler : IRequestHandler<DeletedProductionProcessCommand, int>
    {
        /// <summary>
        /// 工序仓储接口
        /// </summary>
        protected readonly IProductionProcessRepository productionProcessRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionProcessRepository">工序仓储接口</param>
        public DeletedProductionProcessCommandHandler(IProductionProcessRepository productionProcessRepository)
        {
            this.productionProcessRepository = productionProcessRepository;
        }

        /// <summary>
        /// 处理删除工序命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public async Task<int> Handle(DeletedProductionProcessCommand request, CancellationToken cancellationToken)
        {
            var result = await productionProcessRepository.GetModelsAsync(request.ProductionProcessesId);
            if (result != null)
            {
                result.IsDeleted = true;
                result.DeletedBy = request.DeletedBy;
                result.DeletedDate= DateTime.Now;
            }
            else
            {
                throw new ArgumentException("该工序不存在");
            }
            return await productionProcessRepository.UpdateAsync(result);
        }
    }
}
